<template>
    <section class="content">
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true">
              <el-form-item>测点名称：
                <el-select v-model="queryPointlist" clearable placeholder="请选择" @change="selectKey">
                  <el-option v-for="item in queryPoint" :key="item.id" :label="item.name" :value="item.name"></el-option>
                </el-select>
                时间：
                <el-date-picker
                  v-model="value2"
                  align="right"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="选择日期"
                  :picker-options="pickerOptions"
                  @change="timefirst">
                </el-date-picker>
                数据类型：
                <el-select v-model="typeid" clearable placeholder="全部">
                  <el-option v-for="item in type" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                
                <el-button type="primary" @click="inquire">查询</el-button>
              </el-form-item>
            </el-form>
        </el-col>
        <el-col :span="14" class="bc-w">
            <div class="chartLine" id="chartLine"></div>
        </el-col>
    </section>
</template>
<script >
import echarts from "echarts";
export default {
  data() {
    return {
      projectId:'',
      queryPoint:[],
      queryPointlist: null,
      chers:[],
      chartLine: null,
      type:[
        {id:1,name:"设备数据"},
        {id:2,name:"人工数据"}
      ],
      typeid:"",
      value2:'',
      series:[],
      symbolSize:5,
      pickerOptions: {
        //仅当天前时间
        disabledDate(time) {
            return time.getTime() > Date.now() - 8.64e6
        },
        // shortcuts:[
        // {
        //     text: "前一个月",
        //       onClick(picker) {
        //       const start = new Date();
        //       start.setTime(start.getTime() - 3600 * 1000 * 30);
        //       picker.$emit("pick", start);
        //       }
        //   },
        //   {
        //     text: "前半年",
        //     onClick(picker) {
        //       const start = new Date();
        //       start.setTime(start.getTime() - 3600 * 1000 * 365 * 0.5);
        //       picker.$emit("pick", start);
        //     },           
        //   }
        // ]
      },
    };
  },
  methods: {
   timefirst(t) {
      this.value2 = t
    },
    selectKey(id) {
      this.id = id;
      let obj = {};
      obj = this.queryPoint.find(item => {
        return item.id === id;
        this.id = obj.id;
      });
    },
    SegselectKey(id) {
      this.id = id;
      let obj = {};
      obj = this.segments.find(item => {
        return item.id === id;
        this.id = obj.id;
      });
    },
     inquire(){
        let data = {
          projectId: this.projectId,
          date:  this.value2,
          pointName: this.queryPointlist
        }
        this.projectModel.settleData(data).then(res => {
          if(res.status != 200) {
             this.$message({
                showClose: true,
                message: res.msg,
                type: 'warning'
              });
          }
          this.series = res.data.series
          this.chers = res.data
          this.drawCharts();
        }).catch(err => {
          console.log(err.message)
        })
    },
    drawLineChart() {
      this.chartLine = echarts.init(document.getElementById("chartLine"));
      var _this = this
      var series = [];
      for (var i = 0; i < _this.series.length; i++) {
        series.push({
          name: _this.series[i].date,
          data: _this.series[i].data,
        });
      }

      this.chartLine.setOption({
        title: {
          left: "center",
          text: _this.chers.PointName+"历史数据",
          subtext: _this.chers.date
        },
        tooltip: {
          trigger: "axis",
          // formatter:function(params){
          //   return '深度:'+params[0].data[1]+ "<br/>"
          //         + '变化量:'+ params[0].data[0]
          // },
          axisPointer: {
            type: "shadow"
          }
        },
        toolbox: {
            orient: 'vertical',
            show: true,
            itemSize: 16,
            feature: {
              restore: {},
              saveAsImage: {}
            }
          },
        xAxis: [{
          name: "累计变化量",
          type: 'value',
          axisLabel:{formatter:'{value}(mm)'}
        }
        ],
        yAxis: [
          {
            name:"深度",
          type: 'value',
          axisLabel:{formatter:'{value}(m)'}
          }
        ],
        series: [{
            name:series.name,
            type: 'line',
            smooth: false,
            symbolSize: _this.symbolSize,
            data: _this.series[0].data
        }]
      });
    },
    drawCharts() {
      this.drawLineChart();
    },
    date() {          
		        var seperator1 = "-";
            const start = new Date();
            var startYear = start.getFullYear()-1;
            var startMonth = start.getMonth()+1;
            var startDate = start.getDate();
            if (startMonth >= 1 && startMonth <= 9) {
		            startMonth = "0" + startMonth;
		        }
		        if (startDate >= 0 && startDate <= 9) {
		            startDate = "0" + startDate;
            }
            var startDat = startYear + seperator1 + startMonth + seperator1 + startDate;
            this.value2 = startDat
    },
    },
  mounted() {
    this.projectId = this.$route.query.projectId;
    this.date()
    let data = {
      projectId: this.projectId,
      monitorItemId: "13",
      resultType: "3"
    };
    this.projectModel
      .queryPoint(data)
      .then(res => {
        ;
        this.queryPoint = res.data;
        if(this.queryPoint.length > 0) {
            this.queryPointlist = this.queryPoint[0].name;
            this.value2 =  moment(this.queryPoint[0].newData.date,'YYYY-MM-DD HH:mm:ss').format('YYYY-MM-DD');
            this.inquire();
        }
      })
      .catch(err => {
        console.log(err.message);
      });
  }
};
</script>


<style scoped>
.content {
  padding-top: 20px;
  background-color: #ffffff;
  min-height: 680px;
}
.bc-w {
  width: 100%;
  height: calc(100% - 90px);
  padding: 10px;
}
.chart-plane {
  height: 100%;
}
.chartLine {
  width: calc(40% - 20px);
  height: calc(100% - 20px);
  padding: 10px 30%;
}
.select-w-100 {
  width: 100px;
}
</style>